//
const mongoose = require('mongoose');

//
mongoose.connect('mongodb://127.0.0.1:27017/games');

//绑定回调
mongoose.connection.on('open', () => {
  //结构对象
  let MeishiSchema = new mongoose.Schema({
    name: String,
    price: {
      type: Number,
      default: 9.9 //默认值
    },
    is_hun: {
      type: Boolean
    },
    area: {
      type: String,
      required: true//设置必填
    },
    la_level: {
      type: Number,
      enum: [0,1,2,3,4,5]
    }
  });

  //模型对象                        内部创建集合会采用『复数』形式
  let MeishiModel = mongoose.model('meishi', MeishiSchema);

  //批量插入集合
  /**
   * [
   *  {
   *    "_id":  ObjectId("6438f74d3e884f3ec96616bc")
   *    "name": "北京",
   *    "pinyin": "beijing"
   *  },
   * {
   *    "_id":  ObjectId("6438f74d3e884f3ec96616bd")
   *    "name": "成都xxx"
   *  },
   * {
   *    "_id":  ObjectId("6438f74d3e884f3ec96616be")
   *    "name": "重庆"
   *  },
   * ]
   * 
   */
  MeishiModel.insertMany([
    {
      name: "火锅",
      price: 128,
      is_hun: true,
      area: "成都",
      la_level: 1,
    },
    {
      name: "凉拌折耳根",
      price: 9.9,
      is_hun: false,
      area: '云贵川渝',
      la_level: 2,
    },
    {
      name: "臭豆腐",
      is_hun: false,
      area: '长沙',
      la_level: 5
    },
  ], (err, data) => {
    if(err) throw err;
    console.log(data);
    //关闭
    mongoose.connection.close();
  });

});


mongoose.connection.on('error', () => {
  console.log('连接失败~~');
});